package com.qrxl.common.controller;

import com.qrxl.common.JsonResult;
import com.qrxl.common.exception.CustomException;
import com.qrxl.common.vo.DictVo;
import com.qrxl.util.JGAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/common")
@Api(tags = "通用模块接口")
@Slf4j
public class DictController {
    private static final String DICT_BASE_PACKAGE = "com.qrxl.enums";

    @GetMapping("/dict/v1/getDict")
    @ResponseBody
    @ApiOperation(value = "获取枚举字典")
    @ApiImplicitParams({@ApiImplicitParam(name = "enumName", value = "枚举名", required = true),
            @ApiImplicitParam(name = "type", value = "枚举子类型")})
    public JsonResult<List<DictVo>> getDictDataByType(@RequestParam String enumName, Integer type) {
        JGAssert.isMaxLength(enumName, 50, "枚举名称不合法");
        return JsonResult.genJsonResultByOk(getDict(enumName, type));
    }

    private List<DictVo> getDict(String enumName, Integer type) {
        String className = DICT_BASE_PACKAGE + "." + enumName;
        Method method;
        try {
            method = Class.forName(className).getMethod("values");

        } catch (ClassNotFoundException e) {
            try {
                className = "com.qrxl.enums" + "." + enumName;
                method = Class.forName(className).getMethod("values");
            } catch (ClassNotFoundException e2) {
                throw new CustomException(String.format("枚举类反射异常：不存在的枚举类！className=%s", className));
            } catch (NoSuchMethodException e3) {
                throw new CustomException("枚举类反射异常：不存在的方法！method=values");
            }
        } catch (NoSuchMethodException e) {
            throw new CustomException("枚举类反射异常：不存在的方法！method=values");
        }
        try {
            List<DictVo> voList = new ArrayList<>();
            Object invoke = method.invoke(null);
            Object[] list = (Object[]) invoke;
            if (list == null || list.length == 0) {
                return voList;
            }
            for (Object o : list) {
                Object code = o.getClass().getMethod("getCode").invoke(o);
                Object name = o.getClass().getMethod("getName").invoke(o);
                if (type == null) {
                    voList.add(DictVo.builder().code(code).name(name).build());
                } else {
                    if (type.equals(o.getClass().getMethod("getType").invoke(o))) {
                        voList.add(DictVo.builder().code(code).name(name).build());
                    }
                }
            }
            return voList;
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new CustomException(String.format("枚举类反射异常：方法执[%s]行失败！", method.getName()));
        } catch (NoSuchMethodException e) {
            throw new CustomException(String.format("枚举类反射异常：方法执[%s]不存在！", method.getName()));
        }
    }
}
